package com.zmn.plat.business.impl.dingtalk;

import java.util.*;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiWorkrecordAddRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiWorkrecordAddResponse;
import com.taobao.api.ApiException;
import com.zmn.plat.business.interfaces.dingtalk.DingTalkService;
import com.zmn.plat.common.constant.DingTalkConsts;

@Service
public class DingTalkServiceImpl implements DingTalkService {

    @Resource
    DingTalkService dingTalkService;

    private Logger logger = LoggerFactory.getLogger(DingTalkServiceImpl.class);

    @Override
    @Cacheable(cacheNames = "redis1h", key = "'taobao:ding:talk:access:token'")
    public String getDingTalkAccessToken() throws ApiException {
        DefaultDingTalkClient client = new DefaultDingTalkClient(DingTalkConsts.DING_TALK_GET_TOKEN_URL);
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(DingTalkConsts.DING_TALK_APP_KEY);
        request.setAppsecret(DingTalkConsts.DING_TALK_APP_SECRET);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = client.execute(request);
        return response.getAccessToken();
    }

    @Override
    public Boolean sendDingTalkWorkRecord(String userId, Date workRecordTime, String title, String url, String contentTitle, String content) throws ApiException {
        logger.error("发送钉钉消息参数userId" + userId + ":workRecordTime:" + workRecordTime + ":title:" + title + ":url:" + url + ":contentTitle:" + contentTitle + ":content:" + content);
        String assessToken = dingTalkService.getDingTalkAccessToken();
        DingTalkClient client = new DefaultDingTalkClient(DingTalkConsts.DING_TALK_SED_WORK_RECORD_MSG_URL);
        OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
        req.setUserid(userId);
        req.setCreateTime(workRecordTime.getTime());
        req.setTitle(title);
        req.setUrl(url);
        req.setPcOpenType(4L);
        List<OapiWorkrecordAddRequest.FormItemVo> list2 = new ArrayList<>();
        OapiWorkrecordAddRequest.FormItemVo obj3 = new OapiWorkrecordAddRequest.FormItemVo();
        list2.add(obj3);
        obj3.setTitle(contentTitle);
        obj3.setContent(content);
        req.setFormItemList(list2);
        OapiWorkrecordAddResponse rsp = client.execute(req, assessToken);
        Map map = JSONObject.parseObject(rsp.getBody(), Map.class);
        if (!Objects.equals(map.get("errcode"), "0")) {
            return true;
        } else {
            logger.error(map.get("errmsg").toString());
            return false;
        }

    }

}
